import pandas as pd
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
import matplotlib.pyplot as plt

# 读数据
data_df=pd.read_csv('data/customers.csv')
data_df.info()

# 计算K值
x = data_df.iloc[:,[2,4]]
sse =[]
sc=[]

for i in range(2,20):
    km = KMeans(n_clusters=i,max_iter=100,random_state=22)
    y_pred =km.fit_predict(x)
    sse.append(km.inertia_)
    sc.append(silhouette_score(x,y_pred))

plt.plot(range(2,20),sse)
plt.grid()
plt.show()

plt.plot(range(2,20),sc)
plt.grid()
plt.show()